package org.swim.servlet;

import java.io.IOException;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.swim.entity.HelpRequests;
import org.swim.entity.User;
import org.swim.session.HelpRequestManagerRemote;

/**
 * Servlet implementation class ViewHelpRequests
 */
public class ViewHelpRequests extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ViewHelpRequests() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        InitialContext ic;
		request.setAttribute("areThereReceivedSuspendedHelpRequests", false);
        try {
			ic = new InitialContext();
			HelpRequestManagerRemote helpRequestManager = (HelpRequestManagerRemote)ic.lookup("HelpRequestManagerRemote");	
			List<HelpRequests> receivedSuspendedHelpRequests = helpRequestManager.getRecievedSuspendedRequest(user.getId());
			if(receivedSuspendedHelpRequests.size()==0) {
				request.setAttribute("areThereReceivedSuspendedHelpRequests", false);			
			} else {
				request.setAttribute("areThereReceivedSuspendedHelpRequests", true);
			}
			request.setAttribute("receivedSuspendedHelpRequests", receivedSuspendedHelpRequests);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			request.setAttribute("areThereReceivedSuspendedHelpRequests", false);
		}
		request.setAttribute("areThereReceivedApprovedHelpRequests", false);
		try {
			ic = new InitialContext();
			HelpRequestManagerRemote helpRequestManager = (HelpRequestManagerRemote)ic.lookup("HelpRequestManagerRemote");	
			List<HelpRequests> receivedApprovedHelpRequests = helpRequestManager.getRecievedApprovedRequest(user.getId());
			if (receivedApprovedHelpRequests.size()==0) {
				request.setAttribute("areThereReceivedApprovedHelpRequests", false);				
			} else {
				request.setAttribute("areThereReceivedApprovedHelpRequests", true);	
			}
			request.setAttribute("receivedApprovedHelpRequests", receivedApprovedHelpRequests);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			request.setAttribute("areThereReceivedApprovedHelpRequests", false);
		}
		request.setAttribute("areThereSentSuspendedHelpRequests", true);
        try {
			ic = new InitialContext();
			HelpRequestManagerRemote helpRequestManager = (HelpRequestManagerRemote)ic.lookup("HelpRequestManagerRemote");	
			List<HelpRequests> sentSuspendedHelpRequests = helpRequestManager.getSentSuspendedRequest(user.getId());
			if (sentSuspendedHelpRequests.size()==0) {
				request.setAttribute("areThereSentSuspendedHelpRequests", false);				
			} else {
				request.setAttribute("areThereSentSuspendedHelpRequests", true);				
			}
			request.setAttribute("sentSuspendedHelpRequests", sentSuspendedHelpRequests);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			request.setAttribute("areThereSentSuspendedHelpRequests", false);
		}
		request.setAttribute("areThereSentApprovedHelpRequests", false);
        try {
			ic = new InitialContext();
			HelpRequestManagerRemote helpRequestManager = (HelpRequestManagerRemote)ic.lookup("HelpRequestManagerRemote");	
			List<HelpRequests> sentApprovedHelpRequests = helpRequestManager.getSentApprovedRequest(user.getId());
			if(sentApprovedHelpRequests.size()==0) {
				request.setAttribute("areThereSentApprovedHelpRequests", false);
			} else {
				request.setAttribute("areThereSentApprovedHelpRequests", true);			
			}
			request.setAttribute("sentApprovedHelpRequests", sentApprovedHelpRequests);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			request.setAttribute("areThereSentApprovedHelpRequests", false);
		}
        try {
			ic = new InitialContext();
			HelpRequestManagerRemote helpRequestManager = (HelpRequestManagerRemote)ic.lookup("HelpRequestManagerRemote");	
			List<HelpRequests> receivedCompletedHelpRequests = helpRequestManager.getRecievedCompletedRequest(user.getId());
			if(receivedCompletedHelpRequests.size()==0) {
				request.setAttribute("areThereReceivedCompletedHelpRequests", false);
			} else {
				request.setAttribute("areThereReceivedCompletedHelpRequests", true);			
			}
			request.setAttribute("receivedCompletedHelpRequests", receivedCompletedHelpRequests);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (NullPointerException e) {
			request.setAttribute("areThereReceivedCompletedHelpRequests", false);
		}
		request.getRequestDispatcher("WEB-INF/ViewHelpRequests.jsp").forward(request, response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}

}
